package org.zhq.service.impl.center;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.IdWorker;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import org.zhq.enums.YesOrNo;
import org.zhq.mapper.*;
import org.zhq.pojo.OrderItems;
import org.zhq.pojo.OrderStatus;
import org.zhq.pojo.Orders;
import org.zhq.pojo.bo.center.OrderItemsCommentBO;
import org.zhq.pojo.vo.MyCommentVO;
import org.zhq.service.center.MyCommentsService;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class MyCommentsServiceImpl implements MyCommentsService {

    private final OrderItemsMapper orderItemsMapper;
    private final ItemsCommentMapper itemsCommentMapper;
    private final OrderStatusMapper orderStatusMapper;
    private final OrdersMapper ordersMapper;
    private final ItemsCommentsMapperCustom itemsCommentsMapperCustom;

    @Autowired
    public MyCommentsServiceImpl(OrderItemsMapper orderItemsMapper, ItemsCommentMapper itemsCommentMapper, OrderStatusMapper orderStatusMapper, OrdersMapper ordersMapper, ItemsCommentsMapperCustom itemsCommentsMapperCustom) {
        this.orderItemsMapper = orderItemsMapper;
        this.itemsCommentMapper = itemsCommentMapper;
        this.orderStatusMapper = orderStatusMapper;
        this.ordersMapper = ordersMapper;
        this.itemsCommentsMapperCustom = itemsCommentsMapperCustom;
    }

    @Transactional(propagation = Propagation.SUPPORTS)
    @Override
    public List<OrderItems> queryPendingComment(String orderId) {
        Wrapper<OrderItems> condition = new LambdaQueryWrapper<OrderItems>().eq(OrderItems::getOrderId, orderId);
        return orderItemsMapper.selectList(condition);
    }

    @Transactional(propagation = Propagation.REQUIRED)
    @Override
    public void saveComments(String orderId, String userId, List<OrderItemsCommentBO> commentList) {
        for (OrderItemsCommentBO orderItemsCommentBO : commentList) {
            orderItemsCommentBO.setCommentId(IdWorker.getIdStr());
        }
        Map<String, Object> map = new HashMap<>();
        map.put("userId", userId);
        map.put("commentList", commentList);
        itemsCommentsMapperCustom.saveComments(map);

        Orders updateOrder = new Orders();
        updateOrder.setId(orderId);
        updateOrder.setIsComment(YesOrNo.YES.type);
        ordersMapper.updateById(updateOrder);

        OrderStatus updateOrderStatus = new OrderStatus();
        updateOrderStatus.setOrderId(orderId);
        updateOrderStatus.setCommentTime(new Date());
        orderStatusMapper.updateById(updateOrderStatus);

    }

    @Override
    public IPage<MyCommentVO> queryMyComments(String userId, Integer pageNo, Integer pageSize) {
        Page page = new Page(pageNo, pageSize);
        Map<String, Object> map = new HashMap<>();
        map.put("userId", userId);
        return itemsCommentsMapperCustom.queryMyComments(page, map);
    }
}
